package com.htu.courseService.picture.controller;

import cn.hutool.core.map.MapUtil;
import com.htu.courseService.common.vo.R;
import com.htu.courseService.picture.entity.Picture;
import com.htu.courseService.picture.service.PictureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/picture")
@RequiredArgsConstructor
@Slf4j
public class PictureController {

    private final PictureService pictureService;

    @GetMapping("/list")
    public R<List<Picture>> getList(){
        List<Picture> pictureList = pictureService.getPictureList();
        return R.success(pictureList);
    }

    @PostMapping("/upload")
    public R<?> upload(@RequestBody Map<String,String> map){
        String fileUrl = MapUtil.get(map, "fileUrl", String.class);
        pictureService.uploadFile(fileUrl);
        return R.success();
    }

    @DeleteMapping("/delete")
    public R<?> delete(Integer id){
        pictureService.deleteFile(id);
        return R.success();
    }
}
